package patrol.mobile.com.mobilepatrol.activity.office;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.dl7.recycler.listener.OnRecyclerViewItemClickListener;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.adapter.ExaminationAllAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.DateBean;
import patrol.mobile.com.mobilepatrol.bean.ExaminationAllBean;
import patrol.mobile.com.mobilepatrol.bean.OrgListBean;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;
import patrol.mobile.com.mobilepatrol.utils.DatePickerUtil03;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.TimeUtils;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.view.CallBack;
import patrol.mobile.com.mobilepatrol.widget.MySpinner;

public class ExaminationAllActivity extends BaseActivity {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.mySpinner01)
    MySpinner mySpinner01;
    @BindView(R.id.tv_start)
    TextView tvStart;
    private ExaminationAllAdapter mAdapter;
    private String currentTime = "";
    private List<ExaminationAllBean> evaluationDetailBeans;
    private String orgId = "";

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_examination_all;
    }

    @Override
    public void initView() {
        super.initView();
        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        CommonUtil.exitActivityAndBackAnim(ExaminationAllActivity.this, true);
                    }
                });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("岗位考核汇总");
        currentTime = TimeUtils.dateStringYM(System.currentTimeMillis());
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        tvStart.setText(TimeUtils.dateStringYM(System.currentTimeMillis()));
        tvStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                DatePickerUtil.getInstance(ExaminationAllActivity.this).setCallBack(ExaminationAllActivity.this);
                DatePickerUtil03.getInstance().show(ExaminationAllActivity.this, new CallBack<DateBean>() {
                    @Override
                    public void onBack(DateBean dateBean) {
                        currentTime = dateBean.getmDate();
                        tvStart.setText(dateBean.getmDate());
                        requestData(currentTime);
                    }
                });
            }
        });
    }

    @Override
    public void initData() {
        super.initData();

        mAdapter = new ExaminationAllAdapter(this);
        recyclerView.setAdapter(mAdapter);

        getOrg();
    }

    private void getOrg() {
        HashMap<String, String> hashMap = new HashMap();
//        hashMap.put("objType", "分局,总局");
        hashMap.put("orgId", UserManager.getOrgNo());
        HttpSender sender = new HttpSender(this, baseUrl, Urls.getOrgList);
        sender.requestPost(hashMap, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(final String code, String response) {
                final OrgListBean orgListBean = new Gson().fromJson(response, OrgListBean.class);
                List<String> list = new ArrayList<>();
                for (OrgListBean.ListBean listBean : orgListBean.getList()) {
                    list.add(listBean.getOrgName());
                }

                mySpinner01.setmDatas(list, new MySpinner.CallBack() {
                    @Override
                    public void onSelect(int postion) {
                        requestData(currentTime);
                        orgId = orgListBean.getList().get(postion).getId();
                    }
                });
                if (orgListBean.getList().size() == 0) return;
                requestData(currentTime);
            }
        });
    }

    private void requestData(String month) {
        if (TextUtils.isEmpty(orgId)) {
            orgId = UserManager.getOrgNo();
        }
        HttpSender sender = new HttpSender(this, baseUrl, Urls.GET_ORG_POST_TOTAL);
        HashMap<String, String> map = new HashMap<>();
        map.put("orgId", orgId);
        map.put("month", month);
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                JsonParser parser = new JsonParser();
                JsonObject asJsonObject = parser.parse(response).getAsJsonObject();
                JsonArray list = asJsonObject.getAsJsonArray("list");
                evaluationDetailBeans = GsonUtil.jsonToList(list.toString(), ExaminationAllBean.class);
                mAdapter.updateItems(evaluationDetailBeans);
            }
        });
    }

    @Override
    public void setListener() {
        mAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(ExaminationAllActivity.this, ExaminationAllDetailActivity.class);
                ExaminationAllBean examinationAllBean = evaluationDetailBeans.get(position);
                intent.putExtra("data", examinationAllBean);
                intent.putExtra("date", currentTime);
                intent.putExtra("orgId", orgId);
                startActivity(intent);
            }
        });
    }
}
